package com.cynteka.cyfoman.core.mail;

import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Store;
import java.util.Properties;

public class MainClass {

    public static void main(String[] args) throws Exception {

        Properties props = new Properties();

        String host = "pop.gmail.com";
        String username = "sergey.grigorchuk@gmail.com";
        String password = "gfhjkmueuk0331";
        String provider = "pop3";
        int port = 995;

        Session session = Session.getDefaultInstance(props, null);
        Store store = session.getStore(provider);
        store.connect(host, port, username, password);

        Folder inbox = store.getFolder("INBOX");
        if (inbox == null) {
            System.out.println("No INBOX");
            System.exit(1);
        }
        inbox.open(Folder.READ_ONLY);

        Message[] messages = inbox.getMessages();
        for (int i = 0; i < messages.length; i++) {
            System.out.println("Message " + (i + 1));
            messages[i].writeTo(System.out);
        }
        inbox.close(false);
        store.close();
    }
}